package com.bh.extend.service.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bh.common.annotation.KyAbnormal;
import com.bh.common.core.domain.entity.SysUser;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.ReflectionUtils;
import com.bh.common.utils.StringUtils;
import com.bh.extend.domain.JcAbnormalDetail;
import com.bh.extend.mapper.JcAbnormalDetailMapper;
import com.bh.extend.service.IJcAbnormalDetailService;
import com.bh.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * 异常明细Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcAbnormalDetailServiceImpl extends ServiceImpl<JcAbnormalDetailMapper, JcAbnormalDetail> implements IJcAbnormalDetailService {
    @Autowired
    private JcAbnormalDetailMapper jcAbnormalDetailMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询异常明细
     *
     * @param id 异常明细主键
     * @return 异常明细
     */
    @Override
    public JcAbnormalDetail selectJcAbnormalDetailById(Long id) {
        return jcAbnormalDetailMapper.selectById(id);
    }

    /**
     * 查询异常明细列表
     *
     * @param jcAbnormalDetail 异常明细
     * @return 异常明细
     */
    @Override
    public List<JcAbnormalDetail> selectJcAbnormalDetailList(JcAbnormalDetail jcAbnormalDetail) {
        return jcAbnormalDetailMapper.selectJcAbnormalDetailList(jcAbnormalDetail);
    }

    /**
     * 新增异常明细
     *
     * @param jcAbnormalDetail 异常明细
     * @return 结果
     */
    @Override
    public int insertJcAbnormalDetail(JcAbnormalDetail jcAbnormalDetail) {
        jcAbnormalDetail.setCreateTime(DateUtils.getNowDate());
        return jcAbnormalDetailMapper.insert(jcAbnormalDetail);
    }

    /**
     * 修改异常明细
     *
     * @param jcAbnormalDetail 异常明细
     * @return 结果
     */
    @Override
    public int updateJcAbnormalDetail(JcAbnormalDetail jcAbnormalDetail) {
        return jcAbnormalDetailMapper.updateById(jcAbnormalDetail);
    }

    /**
     * 批量删除异常明细
     *
     * @param ids 需要删除的异常明细主键
     * @return 结果
     */
    @Override
    public int deleteJcAbnormalDetailByIds(Long[] ids) {
        return jcAbnormalDetailMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除异常明细信息
     *
     * @param id 异常明细主键
     * @return 结果
     */
    @Override
    public int deleteJcAbnormalDetailById(Long id) {
        return jcAbnormalDetailMapper.deleteById(id);
    }

    @Override
    public List<JcAbnormalDetail> getAbnormalList(Object source, Object target) {
        //目标缓存变量
        Map<String, Field> targerMap = new HashMap<String, Field>();
        List<JcAbnormalDetail> abnormalDetails = new ArrayList<JcAbnormalDetail>();
        Field[] declaredFields = target.getClass().getDeclaredFields();
        for (Field id : declaredFields) {
            targerMap.put(id.getName(), id);
        }
        Field[] fields = source.getClass().getDeclaredFields();
        for (Field field : fields) {
            KyAbnormal annotation = field.getAnnotation(KyAbnormal.class);
            if (annotation != null) {
                //说明注解有
                JcAbnormalDetail abnormalDetail = new JcAbnormalDetail();
                String name = field.getName();
                Field targerField = targerMap.get(name);
                if ("class java.lang.String".equals(field.getGenericType().toString())) { // 如果type是类类型，则前面包含"class "，后面跟类名
                    String sourceValue = (String) ReflectionUtils.invokeGetMethod(source, name, null);
                    String targerValue = (String) ReflectionUtils.invokeGetMethod(target, name, null);
                    if (StringUtils.isEmpty(sourceValue)) {
                        sourceValue = "空";
                    }
                    if (StringUtils.isEmpty(targerValue)) {
                        targerValue = "空";
                    }
                    boolean flag = !sourceValue.equals(targerValue) && (!"0.00".equals(sourceValue) && !"0".equals(sourceValue)) && (!"0.00".equals(targerValue) && !"0".equals(targerValue));
                    if (flag) {
                        abnormalDetail.setSource(name);
                        abnormalDetail.setTarger(annotation.name());
                        abnormalDetail.setSourceValue(sourceValue);
                        abnormalDetail.setTargerValue(targerValue);
                        abnormalDetails.add(abnormalDetail);
                        ReflectionUtils.invokeSetMethod(source, name, new Object[]{targerValue});
                    }
                    continue; //跳出本次循环
                }
                // 如果类型是Integer
                if ("class java.lang.Integer".equals(field.getGenericType().toString()) || "int".equals(field.getGenericType().toString())) {
                    Integer sourceValue = (Integer) ReflectionUtils.invokeGetMethod(source, name, null);
                    Integer targerValue = (Integer) ReflectionUtils.invokeGetMethod(target, name, null);
                    if (sourceValue == null) {
                        sourceValue = 0;
                    }
                    if (targerValue == null) {
                        targerValue = 0;
                    }
                    if (!sourceValue.equals(targerValue)) {
                        abnormalDetail.setSource(name);
                        abnormalDetail.setTarger(annotation.name());
                        String s = annotation.dataBookName();
                        if (StringUtils.isEmpty(s)) {
                            abnormalDetail.setSourceValue(sourceValue.toString());
                            abnormalDetail.setTargerValue(targerValue.toString());
                        } else {
                            abnormalDetail.setSourceValue(getDataBookValue(s, sourceValue.toString()));
                            abnormalDetail.setTargerValue(getDataBookValue(s, targerValue.toString()));
                        }


                        abnormalDetails.add(abnormalDetail);
                        ReflectionUtils.invokeSetMethod(source, name, new Object[]{targerValue.intValue()});
                    }
                    continue; //跳出本次循环

                }

                // 如果类型是Double
                if ("class java.math.BigDecimal".equals(field.getGenericType().toString()) || "BigDecimal".equals(field.getGenericType().toString())) {
                    BigDecimal sourceValue = (BigDecimal) ReflectionUtils.invokeGetMethod(source, name, null);
                    BigDecimal targerValue = (BigDecimal) ReflectionUtils.invokeGetMethod(target, name, null);
                    if (sourceValue == null) {
                        sourceValue = BigDecimal.ZERO;
                    }
                    if (targerValue == null) {
                        targerValue = BigDecimal.ZERO;
                    }
                    if (!sourceValue.equals(targerValue)) {
                        abnormalDetail.setSource(name);
                        abnormalDetail.setTarger(annotation.name());
                        abnormalDetail.setSourceValue(sourceValue.toString());
                        abnormalDetail.setTargerValue(targerValue.toString());
                        abnormalDetails.add(abnormalDetail);
                        ReflectionUtils.invokeSetMethod(source, name, new Object[]{targerValue});
                    }
                    continue; //跳出本次循环
                }
                if ("class java.sql.Timestamp".equals(field.getGenericType().toString())) {
                    Date sourceValue = (Date) ReflectionUtils.invokeGetMethod(source, name, null);
                    Date targerValue = (Date) ReflectionUtils.invokeGetMethod(target, name, null);
                    if (sourceValue == null) {
                        if (targerValue == null) {
                            continue; //跳出本次循环
                        } else {
                            abnormalDetail.setSource(name);
                            abnormalDetail.setTarger(annotation.name());
                            abnormalDetail.setSourceValue("无");
                            abnormalDetail.setTargerValue(targerValue.toString());
                            abnormalDetails.add(abnormalDetail);
                            ReflectionUtils.invokeSetMethod(source, name, new Object[]{targerValue});
                        }
                    }
                    if (sourceValue == null) {
                        abnormalDetail.setSource(name);
                        abnormalDetail.setTarger(annotation.name());
                        abnormalDetail.setSourceValue("空");
                        abnormalDetail.setTargerValue(targerValue.toString());
                        abnormalDetails.add(abnormalDetail);
                        ReflectionUtils.invokeSetMethod(source, name, new Object[]{targerValue});

                    } else {
                        if (!sourceValue.equals(targerValue)) {
                            abnormalDetail.setSource(name);
                            abnormalDetail.setTarger(annotation.name());
                            abnormalDetail.setSourceValue(sourceValue.toString());
                            abnormalDetail.setTargerValue(targerValue.toString());
                            abnormalDetails.add(abnormalDetail);
                            ReflectionUtils.invokeSetMethod(source, name, new Object[]{targerValue});
                        }
                    }

                    continue; //跳出本次循环
                }
                Class<?>[] clszz = annotation.clszz();
                if (annotation.clszz().getClass() != null) {
                    Class<?>[] clszzs = annotation.clszz();
                    Object sourceObj = ReflectionUtils.invokeGetMethod(source, name, null);
                    Object targerObj = ReflectionUtils.invokeGetMethod(target, name, null);
                    if (sourceObj == null && targerObj == null) {
                        continue;
                    }


                    if (sourceObj == null) {
                        String targerid = (String) ReflectionUtils.invokeGetMethod(targerObj, "id", null);
                        String tableName = SqlHelper.table(target.getClass()).getTableName();

                        abnormalDetail.setSource(name);
                        abnormalDetail.setTarger(annotation.name());
                        abnormalDetail.setSourceValue("空");
                        abnormalDetail.setTargerValue(sysDictDataMapper.selectTableDictLabelByValue(tableName, name, "id" , targerid));
                        abnormalDetails.add(abnormalDetail);
                        ReflectionUtils.invokeSetMethod(source, name, new Object[]{null});
                        continue; //跳出本次循环
                    }
                    if (targerObj == null) {
                        String sourceName = (String) ReflectionUtils.invokeGetMethod(sourceObj, annotation.clazzName(), null);
                        abnormalDetail.setSource(name);
                        abnormalDetail.setTarger(annotation.name());
                        abnormalDetail.setSourceValue(sourceName);
                        abnormalDetail.setTargerValue("空");
                        abnormalDetails.add(abnormalDetail);
                        ReflectionUtils.invokeSetMethod(source, name, new Object[]{null});
                        continue; //跳出本次循环
                    }
                    if ("id".equals(name)) {
                        String sourceid = (String) ReflectionUtils.invokeGetMethod(sourceObj, "id", null);
                        String targerid = (String) ReflectionUtils.invokeGetMethod(targerObj, "id", null);

                        boolean equals = sourceid.equals(targerid);

                        if (!equals) {
                            //如果ID不相等
                            String tableName = SqlHelper.table(target.getClass()).getTableName();
                            String sourceName = (String) ReflectionUtils.invokeGetMethod(sourceObj, annotation.clazzName(), null);
                            abnormalDetail.setSource(name);
                            abnormalDetail.setTarger(annotation.name());
                            abnormalDetail.setSourceValue(sourceName);
                            abnormalDetail.setTargerValue(sysDictDataMapper.selectTableDictLabelByValue(tableName, name, "id" , targerid));
                            abnormalDetails.add(abnormalDetail);
                            ReflectionUtils.invokeSetMethod(source, name, new Object[]{null});
                            continue; //跳出本次循环
                        }
                    }
                }


            }
        }

        return abnormalDetails;
    }

    private String getDataBookValue(String dataBook, String value) {
        return sysDictDataMapper.selectJcDictLabelByType(dataBook, value);
    }
}
